package com.laowang.project.system.controller;

import java.util.List;

import com.laowang.common.utils.SecurityUtils;
import com.laowang.project.system.domain.MyCyhShop;
import com.laowang.project.system.service.MyCyhShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.MyCyhLook;
import com.laowang.project.system.service.MyCyhLookService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

/**
 * 商家浏览Controller
 *
 * @author laowang
 * @date 2023-05-30
 */
@Api(tags = "商家浏览接口")
@RestController
@RequestMapping("/shopLook")
public class MyCyhLookController extends BaseController {
    @Autowired
    private MyCyhLookService myCyhLookService;

    @Autowired
    private MyCyhShopService myCyhShopService;

    @ApiOperation(value = "小程序端 添加商家浏览",notes = "")
    @GetMapping("/addLook/{shopId}")
    public AjaxResult addLook(@PathVariable("shopId") Integer shopId ) {

        MyCyhShop myCyhShop = myCyhShopService.selectMyCyhShopById(shopId);
        if (myCyhShop == null){
            return AjaxResult.error("商家信息不存在");
        }
        //Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        //MyCyhLook myCyhLook = new MyCyhLook();
        //myCyhLook.setShopId(shopId);
        //myCyhLook.setUserId(mid);
       /* List<MyCyhLook> myCyhLooks = myCyhLookService.selectMyCyhLookList(myCyhLook);
        if (myCyhLooks.size() <=0){
            myCyhLookService.insertMyCyhLook(myCyhLook);
            synchronized (this){
                Integer lookNum = myCyhShop.getLookNum();
                lookNum++;
                myCyhShop.setLookNum(lookNum);
                myCyhShopService.updateMyCyhShop(myCyhShop);
            }
        }*/
        synchronized (this) {
            Integer lookNum = myCyhShop.getLookNum();
            lookNum++;
            myCyhShop.setLookNum(lookNum);
            myCyhShopService.updateMyCyhShop(myCyhShop);
        }
        return AjaxResult.success();
    }


    /**
     * 查询商家浏览列表
     */
    @ApiOperation(value = "查询商家浏览列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopLook:list')")
    @GetMapping("/list")
        public TableDataInfo list(MyCyhLook myCyhLook) {
        startPage();
        List<MyCyhLook> list = myCyhLookService.selectMyCyhLookList(myCyhLook);
        return getDataTable(list);
    }

    /**
     * 导出商家浏览列表
     */
    @ApiOperation(value = "导出商家浏览列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopLook:export')")
    @Log(title = "商家浏览", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyCyhLook myCyhLook) {
        List<MyCyhLook> list = myCyhLookService.selectMyCyhLookList(myCyhLook);
        ExcelUtil<MyCyhLook> util = new ExcelUtil<MyCyhLook>(MyCyhLook.class);
        return util.exportExcel(list, "shopLook");
    }

    /**
     * 获取商家浏览详细信息
     */
    @ApiOperation(value = "获取商家浏览详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopLook:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(myCyhLookService.selectMyCyhLookById(id));
    }

    /**
     * 新增商家浏览
     */
    @ApiOperation(value = "新增商家浏览",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopLook:add')")
    @Log(title = "商家浏览", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCyhLook myCyhLook) {
        return toAjax(myCyhLookService.insertMyCyhLook(myCyhLook));
    }

    /**
     * 修改商家浏览
     */
    @ApiOperation(value = "修改商家浏览",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopLook:edit')")
    @Log(title = "商家浏览", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCyhLook myCyhLook) {
        return toAjax(myCyhLookService.updateMyCyhLook(myCyhLook));
    }

    /**
     * 删除商家浏览
     */
    @ApiOperation(value = "删除商家浏览",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopLook:remove')")
    @Log(title = "商家浏览", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(myCyhLookService.deleteMyCyhLookByIds(ids));
    }
}
